<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/4/23/023
 * Time: 17:44
 */

namespace app\Lib\Model\User;


use app\Lib\Exception\BlException;
use app\Lib\Model\AdminAuth\GetAuthorization\GetUserAuth;
use app\Lib\Model\DbModel\AdminUserPriAuth;

class EditUser extends  OperationUserBase
{
    /** @var User $user */
    private $user;
    public function setUser(User $user){
        $this->user = $user;
        return $this;
    }
    /**
     * 新增后台用户
     * @return bool
     * @throws BlException
     * @throws \app\Lib\Exception\BlException
     */
    public function run(){
        $data = $this->neatenData();
        $table = $this->getTable();
        unset($data['account']);
        $data[$table->_id] = $this->user->getUid();

        if($this->accountStatus){
            $data[$table->_account_status]  = $this->accountStatus;
        }
        if($this->password){
            $data[$table->_password] = md5(md5($this->prefix.$this->password).$this->prefix);
        }
        $result = $table->update($data);

        if(!$result){
            throw new BlException('修改失败');
        }
        $user = $this->user;
        if($user->getRoleClass()->getRoleId() != $this->role->getRoleId()){
            $userBingRole = (new UserBindingRole($user));
            $userBingRole->setOpUserObj($this->opUser)->setRoleObj($this->role)->run();
        }
        $user->delUserCache();
        $this->handelUserAuth();
        (new GetUserAuth($this->user))->delCache();
        return true ;
    }

    /**
     * @throws BlException
     */
    private function handelUserAuth(){
        $array = (new GetUserAuth($this->user))->run();
        $model = new AdminUserPriAuth();
        $user = $this->user;
        if(empty($this->auth)){
            $result = $model->where([$model->_uid=>$this->user->getUid()])->delete();
            if(!$result){
                throw new BlException('清除私有权限失败');
            }
        }else{
            if(empty($array)){
                foreach ($this->auth as $k=>$v){
                    $newData[$k]=[
                        $model->_create_uid=>$this->opUser->getUid(),
                        $model->_create_time=>time(),
                        $model->_menu_id=>$v,
                        $model->_uid=>$user->getUid(),
                        $model->_status=>1,
                    ];
                }
                $result = $model->insertAll($newData);
                if(!$result){
                    throw new BlException('授权失败');
                }
            }else{
                $roleAuth = [];
                foreach ($array as $v){
                    /**@var \app\Lib\Model\AdminAuth\MenuAuth $v */
                    $roleAuth[$v->getMenuId()] = $v->getMenuId();
                }

                $addArray = array_diff($this->auth,$roleAuth);
                if($addArray){
                    $this->addArray($addArray);
                }
                $delArray = array_diff($roleAuth,$this->auth);
                if($delArray){
                    $this->delArray($delArray);
                }
            }
        }
    }

    /**
     * @param $addArray
     * @return bool
     * @throws BlException
     */
    private function addArray($addArray){
        $model = new AdminUserPriAuth();
        foreach ($addArray as $k=>$v){
            $map[$k] = [
                $model->_uid=>$this->user->getUid(),
                $model->_menu_id=>$v,
                $model->_create_time=>time(),
                $model->_create_uid=>$this->opUser->getUid(),
            ];
        }
        $result = $model->insertAll($map);
        if(!$result){
            throw new BlException('新增部分权限失败');
        }
        return true;
    }

    /**
     * @param $delArray
     * @return bool
     * @throws BlException
     */
    private function delArray($delArray){
        $model = new AdminUserPriAuth();
        foreach ($delArray as $v){
            $map = [
                $model->_uid=>$this->user->getUid(),
                $model->_menu_id=>$v
            ];
            $result = $model->where($map)->delete();
            if(!$result){
                throw new BlException('删除部分权限失败');
            }
        }
        return true;
    }
}